
//
//  NRWLookupHistoryVC.m
//  NRWMall
//
//  Created by 张记 on 2018/6/4.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWLookupHistoryVC.h"
#import "NRWCarListCell.h"
#import "NRWCarDetailVC.h"
#import "FMDBInfo.h"
#import "NRWBuyCarListRectModel.h"

@interface NRWLookupHistoryVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property(nonatomic, strong) NSArray<NRWBuyCarListRectModel *> *carListArr;

@end

@implementation NRWLookupHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationView setTitle:@"浏览历史"];
	
	NSArray *dataArr = [[FMDBInfo sharedDataBase] getAllCarLsit];
	dataArr = (NSMutableArray *)[[dataArr reverseObjectEnumerator] allObjects];
	NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:dataArr.count];
	for (Result *itemModel in dataArr) {
		NRWBuyCarListRectModel *rectModel = [NRWBuyCarListRectModel new];
		rectModel.dataModel = itemModel;
		[arrM addObject:rectModel];
	}
	self.carListArr = arrM.copy;
  
    [self initTableview];
}
#pragma mark - setupUI
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = backColor();
    [self.tableview registerClass:[NRWCarListCell class] forCellReuseIdentifier:@"NRWCarListCell"];
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.tableFooterView = DPView();
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(getNavHeight());
        make.left.right.bottom.equalTo(weakSelf.view);
    }];
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (self.carListArr.count == 0) {
        [self.tableview showMaskViewWithTitle:@"暂无浏览历史" imageName:@"default_empty" titleToImageMargin:42 canScroll:NO didClick:^{
            
        }];
    }
}

#pragma mark - DelegateMethod
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.carListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NRWCarListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWCarListCell" forIndexPath:indexPath];
	cell.carFrameModel = self.carListArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	
    Result *rt = self.carListArr[indexPath.row].dataModel;
    NRWCarDetailVC *DetailVc = [[NRWCarDetailVC alloc] init];
    DetailVc.carId = rt.carId;
    DetailVc.thisCarModel = rt;
    [self.navigationController pushViewController:DetailVc animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
